package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.R;

public class UserInfoActivity extends AppCompatActivity {

    private ImageView info_back,info_head;
    private EditText info_name,info_phone,info_address,info_age;
    private RadioButton info_boy,info_girl;
    private RadioGroup info_sex;
    private Context context;
    private Button modify;
    public static final int MODIFIED=0;
    private String name;
    private String age1;
    private int age;
    private String phone;
    private String adress;
    private String finalSex;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        //根据ID找到相关控件
        findByID();
        //设置返回按钮
        info_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UserInfoActivity.this,AppSetActivity.class);
                startActivity(intent);
            }
        });

        //设置个人信息
        setUserInfo();
        //设置修改按钮
        modifyInfo();

    }

    private void setUserInfo() {

        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        if(sp != null && !sp.getString("name","").equals("")){
            String name = sp.getString("name","");
            String sex = sp.getString("sex","");
            int age = sp.getInt("age",1);
            String phone = sp.getString("phone","");
            String address = sp.getString("address","");
            account = sp.getString("account","");

            //设置信息显示
            info_name.setText(name);
            if(sex.equals("男")){
                info_boy.setChecked(true);
                info_head.setImageResource(R.mipmap.boy_head);
            }else{
                info_girl.setChecked(true);
                info_head.setImageResource(R.mipmap.girl_head);
            }
            info_age.setText(age+"");
            info_phone.setText(phone);
            info_address.setText(address);
        }else{
            Toast.makeText(context,"小橙检测到您还没有登录哦！！！",Toast.LENGTH_SHORT).show();
        }

    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MODIFIED:
                    Boolean check = (Boolean) msg.obj;
                    if(check){
                        Toast.makeText(UserInfoActivity.this,"修改成功！",Toast.LENGTH_SHORT).show();
                        SharedPreferences sp = context.getSharedPreferences("userInfo",Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("name",name);
                        editor.putString("sex",finalSex);
                        editor.putInt("age",age);
                        editor.putString("phone",phone);
                        editor.putString("address",adress);
                        editor.commit();
                        //将信息重新设置到个人资料中
                        setUserInfo();
                    }else{
                        Toast.makeText(context,"修改失败",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }

        }
    };

    public void modifyInfo(){

        //确认修改按钮设置点击事件
        modify.setOnClickListener(new View.OnClickListener() {
            public void getData(){
                name = info_name.getText().toString();
                String sex1 = null;
                if(info_boy.isChecked()){
                    sex1 = "男";
                }else {
                    sex1 = "女";
                }
                age1 = info_age.getText().toString();
                age = Integer.parseInt(age1);
                phone = info_phone.getText().toString();
                adress = info_address.getText().toString();
                finalSex = sex1;
            }
            @Override
            public void onClick(View v) {
                getData();
                OkHttps.async("http://10.150.11.9:8086/chengcar/modify/")
                        .addUrlPara("name", name)
                        .addUrlPara("sex", finalSex)
                        .addUrlPara("age", age)
                        .addUrlPara("phone", phone)
                        .addUrlPara("address", adress)
                        .addUrlPara("account",account)
                        .setOnResBean(Boolean.class,check ->{
                            Message message = Message.obtain();
                            message.what = MODIFIED;
                            message.obj = check;
                            handler.sendMessage(message);
                        })
                        .get();
            }
        });


    }

    public void findByID(){
        context = getBaseContext();
        info_back = this.findViewById(R.id.info_back);
        info_head = this.findViewById(R.id.info_head);
        info_name = this.findViewById(R.id.info_name);
        info_age = this.findViewById(R.id.info_age);
        info_phone = this.findViewById(R.id.info_phone);
        info_address = this.findViewById(R.id.info_address);
        info_sex = this.findViewById(R.id.info_sex);
        info_boy = this.findViewById(R.id.info_boy);
        info_girl = this.findViewById(R.id.info_girl);
        modify = this.findViewById(R.id.info_ok);
    }

}